package rinpu.springtest.pojo.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @Author Rin_pu
 * @Date 2025/9/16 20:44
 * @Description SecurityUser 符合Spring Security的用户信息类
 */

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SecurityUser implements UserDetails {

    private User user;
    // 对应的token存放redis中的id
    private String token;
    // 登录时间
    private Long loginTime;
    // 过期时间
    private Long expireTime;
    // 权限
    private Set<Permission> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (Permission permission : permissions) {
            authorities.add(new SimpleGrantedAuthority(permission.getPermission()));
        }
        return authorities;
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @JsonProperty
    @Override
    public boolean isEnabled() {
        return true;
    }
}
